package com.luhuiguo.cms.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.HasText;
import com.google.inject.Inject;
import com.luhuiguo.cms.client.AppConstants;
import com.luhuiguo.cms.client.AppMessages;
import com.luhuiguo.cms.shared.event.LoginEvent;
import com.luhuiguo.cms.shared.event.LoginEventHandler;
import com.luhuiguo.cms.shared.model.UserModel;
import com.luhuiguo.gwt.dispatcher.client.DispatcherAsync;
import com.luhuiguo.gwt.presenter.client.EventBus;
import com.luhuiguo.gwt.presenter.client.widget.WidgetDisplay;
import com.luhuiguo.gwt.presenter.client.widget.WidgetPresenter;

public class HeaderPresenter extends WidgetPresenter<HeaderPresenter.Display> {

    public interface Display extends WidgetDisplay {
        public HasClickHandlers getLogoutClick();

        public HasClickHandlers getAboutClick();

        public HasText getUserText();

    }

    @SuppressWarnings("unused")
    private UserModel user;
    @SuppressWarnings("unused")
    private AppConstants constants;

    private AppMessages messages;
    @SuppressWarnings("unused")
    private DispatcherAsync dispatcher;

    @Inject
    public HeaderPresenter(Display display, EventBus eventBus, DispatcherAsync dispatcher, AppConstants constants,
            AppMessages messages) {
        super(display, eventBus);
        this.dispatcher = dispatcher;
        this.constants = constants;
        this.messages = messages;
        GWT.log("HeaderPresenter created");
    }

    @Override
    protected void onBind() {
        GWT.log("HeaderPresenter onBind");

        registerHandler(eventBus.addHandler(LoginEvent.TYPE, new LoginEventHandler() {

            public void onLogin(LoginEvent event) {
                user = event.getUser();
                display.getUserText().setText(messages.welcome(event.getUser().getUsername()));
            }

        }));

        registerHandler(display.getAboutClick().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                doAbout();
            }
        }));

    }

    @Override
    protected void onRevealDisplay() {
        GWT.log("HeaderPresenter onRevealDisplay");
    }

    @Override
    protected void onUnbind() {
        GWT.log("HeaderPresenter onUnbind");

    }

    private void doAbout() {

    }

}
